<?php

namespace app\admin\controller;

use think\Request;
use app\admin\model\AdminRoleModel;
use think\Validate;
use think\Db;
use app\admin\model\AdminMenuModel;

class AdminRole extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    //角色列表
    public function index()
    {
        $admin_role_model = new AdminRoleModel;
        $role = $admin_role_model->getAllRole();
        return $this->fetch('', [
            'data' => $role,
        ]);
    }

    /**
     * 添加角色
     * @param Request $request
     * @return mixed|\think\response\Json
     */
    public function roleAdd(Request $request)
    {
        $admin_role_model = new AdminRoleModel;
        $admin_menu_model = new AdminMenuModel;
        if ($request->isAjax()) {
            $validate = new Validate([
                'name|角色名称' => 'require|length:1,10|token',
            ]);
            if ($validate->check($request->param())) {
                $res = $admin_role_model->roleAdd($request->param());
                if ($res['status'] == 'n') {
                    $res['token'] = $request->token();
                }
                return json($res);
            } else {
                return json(['status' => 'n', 'info' => $validate->getError(), 'token' => $request->token()]);
            }
        }

        //权限数据
        $menu_tree = $admin_menu_model->getMenuTree();
        //处理层级数据
        global $pid;
        foreach ($menu_tree as $k => $v) {
            if ($v['level'] == 1) {
                $top_id = $v['id'];

                if ($v['status'] != 1) {
                    unset($menu_tree[$k]);
                    $pid = $v['id'];
                }
            }
            if ($v['level'] >= 2) {
                $menu_tree[$k]['top_id'] = $top_id;

                if (!empty($pid) && $v['pid'] == $GLOBALS['pid']) {
                    unset($menu_tree[$k]);
                }

                if ($v['status'] != 1) {
                    unset($menu_tree[$k]);
                }
            }
        }

        return $this->fetch('', [
            'menu_tree' => $menu_tree
        ]);
    }

    public function roleEdit(Request $request)
    {
        $admin_role_model = new AdminRoleModel;
        $admin_menu_model = new AdminMenuModel;
        if ($request->isAjax()) {
            $validate = new Validate([
                'name|角色名称' => 'require|length:1,10|token',
            ]);
            if ($validate->check($request->param())) {
                $res = $admin_role_model->roleEdit($request->param());
                if ($res['status'] == 'n') {
                    $res['token'] = $request->token();
                }
                return json($res);
            } else {
                return json(['status' => 'n', 'info' => $validate->getError(), 'token' => $request->token()]);
            }
        }

        //权限数据
        $menu_tree = $admin_menu_model->getMenuTree();
        //当前角色数据
        $role = $admin_role_model->getRole($request->param('id'));
        if (!empty($role['auth'])) {
            $auth = explode(',', $role['auth']);
        } else {
            $auth = [];
        }

        //处理层级数据
        global $pid;
        foreach ($menu_tree as $k => $v) {
            if (in_array($v['id'], $auth)) {
                $menu_tree[$k]['selected'] = 1;
            } else {
                $menu_tree[$k]['selected'] = 0;
            }
            if ($v['level'] == 1) {
                $top_id = $v['id'];
                if ($v['status'] != 1) {
                    unset($menu_tree[$k]);
                    $pid = $v['id'];
                }
            }
            if ($v['level'] >= 2) {
                $menu_tree[$k]['top_id'] = $top_id;
                if (!empty($pid) && $v['pid'] == $GLOBALS['pid']) {
                    unset($menu_tree[$k]);
                }

                if ($v['status'] != 1) {
                    unset($menu_tree[$k]);
                }
            }
        }
        return $this->fetch('', [
            'menu_tree' => $menu_tree,
            'data' => $role,
        ]);
    }


    /**
     * 删除菜单
     * @param Request $request
     * @return \think\response\Json
     */
    public function roleDel(Request $request)
    {
        $admin_role_model = new AdminRoleModel;
        $id = $request->param('id');
        //获取菜单数据
        $role = $admin_role_model->getRole($id);
        if (empty($role)) {
            return json(['status' => 'n', 'info' => '角色数据不存在']);
        }
        $res = $admin_role_model->where('id', $id)->delete();
        if ($res) {
            return json(['status' => 'y', 'info' => '删除成功！']);
        } else {
            return json(['status' => 'n', 'info' => '删除失败！']);
        }
    }

}
